<?php
namespace app\index\controller;
use think\Controller;
use think\Request;
use app\index\model\Userinfo1;
use think\Db;
class Userinfo extends Common
{
	/*
	*	查询用户信息接口： (Userinfo/index)
	*	请求方式：  get/post
	*	参数：
			APPID
			APPKEY
			UID
	*	请求方式： get/post
	*	返回格式： json 
	*	
	*/
	public function index(Request $request)
	{
		$uid = $request->param("uid");
		if(!isset($uid)){
        	$arr['status'] = 30001;
        	$arr['msg'] = "参数错误";
        }else{
        	$model = new Userinfo1;
	    	$data = $model->show($uid);
	    	$data['idCard'] = substr($data['idCard'], 0, 3).'***********'.substr($data['idCard'], 14);
	    	$data['tel1'] = substr($data['account'], 0, 3).'****'.substr($data['account'],7,4);
	    	$arr['status'] = 200;
	    	$arr['msg'] = "成功";
	    	$arr['data'] = $data;
        }
        echo callback(self::$callback,$arr);
	}
	//修改手机号
	public function updateTel(Request $request)
	{
		$model = new Userinfo1;
		$tel = $request->param("tel");
		if(!preg_match("/^[1][3,4,5,7,8][0-9]{9}$/", $tel)){
			$arr['status'] = 500004;
			$arr['msg'] = '手机格式错误';
		}else{
			$uid = $request->param("uid");
			$res = $model->updateTel($uid,$tel);
			if($res){
				$arr['status'] = 200;
				$arr['msg'] = '成功';
			}else{
				$arr['status'] = 10003;
				$arr['msg'] = '失败';
			}
		}
		echo callback(self::$callback,$arr);
	}
	//修改登陆密码
	public function updatePwd(Request $request)
	{
		$model = new Userinfo1;
		$newPassword = $request->param("newPassword");
		if(!preg_match("/^[A-Za-z0-9]{6,20}$/", $newPassword)){
			$arr['status'] = 500004;
			$arr['msg'] = '密码格式错误';
		}else{
			$uid = $request->param("uid");
			$res = $model->updatePwd($uid,md5($newPassword));
			if($res){
				$arr['status'] = 200;
				$arr['msg'] = '成功';
			}else{
				$arr['status'] = 10003;
				$arr['msg'] = '失败';
			}
		}
		echo callback(self::$callback,$arr);
	}
	//修改登录密码
	public function updatePaypwd(Request $request)
	{
		$uid = $request->param("uid");
		$pwd = $request->param("pwd");
		$len = strlen($pwd);
		if($len!=32){
			$arr['status'] = 301;
			$arr['msg'] = '密码格式有误';
			echo callback(self::$callback,$arr);
		}
		$sql = "update user set payPwd='$pwd' where userid=$uid";
		$res = Db::query($sql);
		$arr['status'] = 200;
		$arr['msg'] = '成功';
		echo callback(self::$callback,$arr);
	}
	//验证支付订单密码
	public function reduce(Request $request)
	{
		$uid = $request->param("uid");
		$money = $request->param("PayMoney");
		$paypwd = $request->param("payPwd");
		$data = Db("user")->where(array("userid"=>$uid,"payPwd"=>$paypwd))->find();
		if(!empty($data)){
			$res = Db::table("user")
			->where("userid",$uid)
			->setDec("money",$money);
			if($res){
				$arr['status'] = 200;
				$arr['msg'] = '成功';
			}
		}else{
			$arr['status'] = 3001;
			$arr['msg'] = '失败';
		}
		echo callback(self::$callback,$arr);
	}
	//还款
	public function refund(Request $request)
	{
		$uid = $request->param("uid");
		$periods = $request->param("periods");
		$b_id = $request->param("b_id");
		$money = $request->param("money");
		$pwd = $request->param("pwd");
		$data = Db("user")->where(array("userid"=>$uid,"payPwd"=>$pwd))->find();
		$arr = [];
		if(!empty($data)){
			$res = Db::table("user")
			->where("userid",$uid)
			->setDec("money",$money);
			if($res){
				$sql="update payhistory set status=2 where b_id = '$b_id' and periods = '$periods'";
				Db::query($sql);
					$arr['status'] = 200;
					$arr['msg'] = '成功';
			}else{
				$arr['status'] = '302';
				$arr['msg'] = '余额不足';
			}
		}else{
			$arr['status'] = 301;
			$arr['msg']= '失败';
		}
		echo callback(self::$callback,$arr);
	}
}
